<template>
  <div>
    <TopFilter :list="topFilterList" :cols="3" labelWidth="100" @filterChange="changeHandle"></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :isSelectColumn="false"
      :isMemoryPagination="true"
      :isPagination="false"
      :dataSource="dataSource"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <multiuse-button size="small" type="primary" :click="exportHandle" auth-mark="/api/cluecenter/jk/clue/exportClueByExcel">导出</multiuse-button>
      </template>
    </FilterTable>
    <!-- 来源渠道弹窗 -->
    <BaseDialog title="来源渠道查询" :visible.sync="visibleSource" width="1000px" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <sourceMadal ref="addDialogRef" selectType="single" @close="closeSource" />
    </BaseDialog>
  </div>
</template>

<script>
import { clueReport, exportClueReport } from '@/api/potenCus/customer/potenCusManageOEM';
import { dictionary } from '@/utils/dictMixin';
import { authority } from '@/utils/authMixin';
import { exportExcelFile } from '@/utils/index';
import { formatDates, formatDat } from '@/utils/validate';
import sourceMadal from '@/pages/cms-common/source/index';

export default {
  name: 'ClueReport',
  components: {
    sourceMadal
  },
  data() {
    return {
      topFilterList: this.createTopFilters(),
      columns: this.createTableColumns(),
      fetchApi: clueReport,
      fetchParams: {},
      selectedRows: [], //选中列表中的行
      visible: false,
      visibleDealer: false,
      UploadFileIsShow: false,
      UploadFileIsShow_upload: false,
      formData: {},
      handleType: '',
      formDataDL: {},
      dialogTitle: '',
      form: {
        fileList: [],
        fileList_upload: []
      },
      dataSource: [],
      params: {
        orgId: 0,
        type: 10041002
      },
      sanjiao: 'sanjiao',
      visibleSource: false, //来源渠道是否显示
      clueSource: '',
      clueSourceName: '',
      dataSourceList: '',
      selectClueIds: [] // 当前所选线索
    };
  },
  created() {},
  methods: {
    createTopFilters() {
      return [
        {
          type: 'INPUT',
          label: '来源渠道',
          fieldName: 'clueSourceName',
          placeholder: '请选择',
          initialValue: '',
          unitRender: () => {
            return (
              <div>
                <el-button slot="append" icon="el-icon-search" onClick={() => this.sourceHandle()}></el-button>
                <el-button slot="append" icon="el-icon-delete" onClick={() => this.clearSourceHandle()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'RANGE_DATE',
          label: '线索创建日期',
          fieldName: 'startTime|endTime',
          placeholder: '请选择'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },

        {
          title: '渠道',
          dataIndex: 'sourceName',
          sorter: true,
          align: 'center',
          filterType: 'input',
          filterItems: []
          // render: props => {
          //   return <span>{this.createDictText(props.row.clueLevel, '7018')}</span>;
          // }
        },
        {
          title: '入库量',
          dataIndex: 'zongshu',
          sorter: true,
          align: 'center',
          summation: true,
          filterType: 'number',
          precision: 2,
          numberFormat: true
        },
        {
          title: '下发总量',
          dataIndex: 'sumSend',
          align: 'center',
          summation: true,
          filterType: 'number',
          precision: 2,
          numberFormat: true
        },
        {
          title: '重复量',
          dataIndex: 'chongfu',
          align: 'center',
          summation: true,
          filterType: 'number',
          precision: 2,
          numberFormat: true
        },
        {
          title: '可跟进量',
          dataIndex: 'canTouch',
          align: 'center',
          summation: true,
          filterType: 'number',
          precision: 2,
          numberFormat: true
        },

        {
          title: '已跟进量',
          dataIndex: 'touched',
          filter: true,
          align: 'center',
          summation: true,
          filterType: 'number',
          precision: 2,
          numberFormat: true
        },

        {
          title: '未跟进量',
          dataIndex: 'noTouch',
          align: 'center',
          sorter: true,
          summation: true,
          filterType: 'number',
          precision: 2,
          numberFormat: true
        },
        {
          title: '首次跟进率',
          dataIndex: 'firstTouchRate',
          align: 'center',
          filterType: 'input'
        },
        {
          title: '累计有效量',
          dataIndex: 'sumED',
          align: 'center',
          summation: true,
          filterType: 'number',
          precision: 2,
          numberFormat: true
        },
        {
          title: '到店量',
          dataIndex: 'toShop',
          align: 'center',
          summation: true,
          filterType: 'number',
          precision: 2,
          numberFormat: true
        },
        {
          title: '订单量',
          dataIndex: 'orderSum',
          align: 'center',
          summation: true,
          filterType: 'number',
          precision: 2,
          numberFormat: true
        },
        {
          title: '成交量',
          dataIndex: 'yichengjiao',
          align: 'center',
          summation: true,
          filterType: 'number',
          precision: 2,
          numberFormat: true
        },
        {
          title: '有效率',
          dataIndex: 'youxiaolv',
          align: 'center',
          filterType: 'input'
        },
        {
          title: '成交率',
          dataIndex: 'chengjiaolv',
          align: 'center',
          filterType: 'input'
        },

        {
          title: '战败量',
          dataIndex: 'failSum',
          align: 'center',
          summation: true,
          filterType: 'number',
          precision: 2,
          numberFormat: true
        },

        {
          title: '战败率',
          dataIndex: 'zhanbailv',
          align: 'center',
          filterType: 'input'
        },

        {
          title: '重复率',
          dataIndex: 'sameTax',
          align: 'center',
          filterType: 'input'
        }
      ];
    },
    // 导出
    async exportHandle(row) {
      // console.log('选择行', this.selectedRows);
      let params = JSON.parse(JSON.stringify(this.fetchParams));

      this.handleType = 'export';
      await exportClueReport(params)
        .then(response => {
          // console.log(response,"response")
          let name = '线索统计报表 ' + (params.startTime ? params.startTime : '') + '-' + (params.endTime ? params.endTime : '') + '  ';
          exportExcelFile(response.data, name);
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 潜客来源
    async getClueTree() {
      const res = await queryClueSourceList();
      // // console.log('潜客来源', res)
      let treeList = res.data;
      treeList.forEach(el => (el.text = el.clueName));
      treeList.forEach(el => (el.value = el.id));
      treeList.forEach(el => (el.parentId = el.parentId));
      //只有叶子节点可以点击
      // let treeList = this.deepListClone([].concat(res.data));
      this.topFilterList.find(item => item.fieldName === 'clueSource').itemList = treeList;
    },
    // 来源渠道关闭窗口 -返回数据
    closeSource(data) {
      console.log('-=-=-=-=-=-=-=', data);
      if (data.data && data.data.length > 0) {
        //循环返回数据
        let clueSourceArray = [];
        let clueSourceNameArray = [];
        data.data.forEach(x => {
          clueSourceArray.push(x.id);
          clueSourceNameArray.push(x.clueName);
        });
        this.clueSource = clueSourceArray.join(',');
        this.clueSourceName = clueSourceNameArray.join(',');
        //显示来源渠道名称
        this.topFilterList.find(item => item.fieldName === 'clueSourceName').initialValue = this.clueSourceName;
      }
      //关闭弹窗
      this.visibleSource = data.visible;
    },
    // 来源渠道打开窗口 -传数据
    sourceHandle() {
      this.visibleSource = true;
    },
    // 来源渠道清除
    clearSourceHandle() {
      this.clueSource = '';
      this.clueSourceName = '';
      this.topFilterList.find(item => item.fieldName === 'clueSourceName').initialValue = '';
    },
    // 来源渠道关闭窗口 -返回数据
    closeSource(data) {
      console.log('-=-=-=-=-=-=-=', data);
      if (data.data && data.data.length > 0) {
        //循环返回数据
        let clueSourceArray = [];
        let clueSourceNameArray = [];
        data.data.forEach(x => {
          clueSourceArray.push(x.id);
          clueSourceNameArray.push(x.clueName);
        });
        this.clueSource = clueSourceArray.join(',');
        this.clueSourceName = clueSourceNameArray.join(',');
        //显示来源渠道名称
        this.topFilterList.find(item => item.fieldName === 'clueSourceName').initialValue = this.clueSourceName;
      }
      //关闭弹窗
      this.visibleSource = data.visible;
    },
    async changeHandle(val) {
      console.log('val', val);
      //this.fetchParams = Object.assign({}, val);
      this.fetchParams = Object.assign({}, val, { clueSourceIds: this.clueSource });
      this.$refs.table.START_LOADING();
      let res = await clueReport(this.fetchParams);
      if (res.resultCode == 200) {
        this.dataSource = res.data;
      }
      this.$refs.table.STOP_LOADING();
    }
  },

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  }
};
</script>
<style lang="less" scoped>
.DepartmentWrap {
  // height: 79vh;
  height: 26vw;
  border: 1px solid #000;
  box-sizing: border-box;
  overflow: hidden;
  .leftTree {
    width: 36%;
    height: 100%;
    overflow-x: hidden;
    overflow-y: scroll;
    float: left;
    padding: 10px;
    box-sizing: border-box;
    position: relative;
  }

  .rightContent {
    width: 64%;
    height: 100%;
    overflow-x: hidden;
    overflow-y: scroll;
    float: left;
    box-sizing: border-box;
    padding: 10px;
    border-left: 2px solid #eee;
  }
}
.sanjiao {
  position: absolute;
  left: 18px;
  top: 19px;
  z-index: 99;
  width: 0;
  height: 0;
  border-top: 3px solid transparent;
  border-bottom: 5px solid transparent;
  border-left: 9px solid #ccc;
}
</style>
